package com.talent.jiwen.wrongbook;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.http.result.tiku.SubjectBean;
import com.talent.jiwen.util.Constant;
import com.talent.jiwen.util.MyToast;
import com.talent.jiwen.util.SPConstant;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

/**
 * Created by qyqx on 2018/7/2.
 */

public class WrongBookActivity extends BaseActivity {
    @BindView(R.id.addLayout)
    RelativeLayout addLayout;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private List<SubjectBean> dataList = new ArrayList<>();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_wrongbook;
    }

    @Override
    public String titleName() {
        return getString(R.string.wrongbook);
    }

    @Override
    protected void initData() {
        setHeadVisibility(View.VISIBLE);

        LinearLayoutManager layoutManager = new LinearLayoutManager(WrongBookActivity.this);
        recycleView.setLayoutManager(layoutManager);
        recycleView.setAdapter(new CommonAdapter<SubjectBean>(this , R.layout.item_wrongbook_subject , dataList) {
            @Override
            protected void convert(ViewHolder holder, final SubjectBean data, int position) {
                holder.setBackgroundRes(R.id.subjectIv , getSubjectImg(data.getKecen_name()));
                holder.setText(R.id.subjectNameTv , data.getKecen_name());

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        WrongListActivity.startWrongListActivity(WrongBookActivity.this , data.getKecen_id() + "");
                    }
                });
            }
        });

        getSubjectList();
    }

    @OnClick(R.id.addLayout)
    public void onViewClicked() {
        WrongImgActivity.startWrongImgActivity(WrongBookActivity.this);
    }


    /**获取学科列表
     *
     */
    private void getSubjectList(){
        String grade;
        if (SPConstant.isMidSchool()) {
            grade = Constant.TIKU_MIDDLE_GRADE;
        } else {
            grade = Constant.TIKU_PRIMARY_GRADE;
        }
        Observable observable = ApiFactory.getTiKuApiSingleton().getSubject(grade);

        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<List<SubjectBean>>(mContext) {
            @Override
            protected void _onNext(List<SubjectBean> listBean) {
                dataList.clear();
                dataList.addAll(listBean);

                recycleView.getAdapter().notifyDataSetChanged();
            }

            @Override
            protected void _onError(String message) {
                MyToast.show(WrongBookActivity.this , message);

            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, true);

}


    /**获取学科图片
     *
     * @param subjectName
     * @return
     */
    private int getSubjectImg(String subjectName) {
        int imgResouseId = R.mipmap.iv_zhengzhi;
        if (subjectName.contains(getString(R.string.yuwen))) {
            imgResouseId = R.mipmap.iv_yuwen;
        } else if (subjectName.contains(getString(R.string.shuxue))) {
            imgResouseId = R.mipmap.iv_shuxue;
        } else if (subjectName.contains(getString(R.string.yingyu))) {
            imgResouseId = R.mipmap.iv_yingyu;
        } else if (subjectName.contains(getString(R.string.huaxue))) {
            imgResouseId = R.mipmap.iv_huaxue;
        } else if (subjectName.contains(getString(R.string.wuli))) {
            imgResouseId = R.mipmap.iv_wuli;
        } else if (subjectName.contains(getString(R.string.shengwu))) {
            imgResouseId = R.mipmap.iv_shenwu;
        } else if (subjectName.contains(getString(R.string.dili))) {
            imgResouseId = R.mipmap.iv_dili;
        } else if (subjectName.contains(getString(R.string.lishi))) {
            imgResouseId = R.mipmap.iv_lishi;
        } else if (subjectName.contains(getString(R.string.siping))) {
            imgResouseId = R.mipmap.iv_zhengzhi;
        } else{
            imgResouseId = R.mipmap.iv_zhengzhi;
        }
        return imgResouseId;
    }

    /**启动该页面
     *
     * @param context
     */
    public static void startWrongBookActivity(Context  context){
        Intent intent = new Intent();
        intent.setClass(context , WrongBookActivity.class);
        context.startActivity(intent);

    }
}
